<table>
	<caption>
		<label for="recordType"><g:message code="b2b.salesOrder.recordType" default="Record Type" /></label>:
		${salesOrderInstance?.recordType?.encodeAsHTML()}
		<g:hiddenField name="recordType.id" value="${salesOrderInstance?.recordType?.id}" />
	</caption>
	<tbody>
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.dealer" default="Dealer" /></td>
            <td  class="value">${salesOrderInstance?.dealer?.encodeAsHTML()}</td>
            <td  class="name"><g:message code="b2b.salesOrder.department" default="Department" /></td>
            <td  class="value">${salesOrderInstance?.dealer?.department?.encodeAsHTML()}</td>
            <td  class="name"><g:message code="b2b.salesOrder.orderDate" default="Order Date" /></td>
            <td  class="value"><g:formatDate format="yyyy-MM-dd" date="${salesOrderInstance?.orderDate}" /></td>
        </tr>
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.industry" default="Industry" /></td>
            <td  class="value"><g:link controller="channel" action="show" id="${salesOrderInstance?.industry?.id}">${salesOrderInstance?.industry?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.project" default="Project" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "project")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.effectiveDate" default="Effective Date" /></td>
            <td  class="value"><g:formatDate format="yyyy-MM-dd" date="${salesOrderInstance?.effectiveDate}" /></td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.id" default="Id" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "id")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.serialNumber" default="ERP Order Number" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "serialNumber")}</td>
            <td  class="name"><g:message code="b2b.salesOrder.accountSerialNumber" default="Account Order Number" /></td>
            <td  class="value">${fieldValue(bean: salesOrderInstance, field: "accountSerialNumber")}</td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.salesMan" default="Sales Man" /></td>
            <td  class="value"><g:link controller="staff" action="show" id="${salesOrderInstance?.salesMan?.id}">${salesOrderInstance?.salesMan?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.status" default="Status" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "status")}</td>
            <td class="name">&nbsp;</td>
            <td class="value">&nbsp;</td>
        </tr>

        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.address1.label" default="Address(1)" /></td>            
            <td  class="value" colspan="7">${salesOrderInstance?.address1?.encodeAsHTML()}</td>
        </tr>

        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.address2.label" default="Address(2)" /></td>            
            <td  class="value" colspan="7">${salesOrderInstance?.address2?.encodeAsHTML()}</td>
        </tr>        
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.description" default="Desc" /></td>
            <td  class="value" colspan="7">${fieldValue(bean: salesOrderInstance, field: "description")}</td>
        </tr>
        
        <tr class="prop">
            <td  class="name"><g:message code="b2b.salesOrder.owner" default="Owner" /></td>
            <td  class="value"><g:link controller="user" action="show" id="${salesOrderInstance?.owner?.id}">${salesOrderInstance?.owner?.encodeAsHTML()}</g:link></td>
            <td class="name"><g:message code="b2b.salesOrder.createdBy" default="Created By" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "createdBy")},${fieldValue(bean: salesOrderInstance, field: "dateCreated")}</td>
            <td class="name"><g:message code="b2b.salesOrder.lastModifiedBy" default="Last Modified By" /></td>
            <td class="value">${fieldValue(bean: salesOrderInstance, field: "lastModifiedBy")},${fieldValue(bean: salesOrderInstance, field: "lastUpdated")}</td>
        </tr>
        
    </tbody>
</table>

<table>
    <thead>
        <tr>
            <th><g:message code="b2b.salesOrderDetail.serialNumber" default="Serial Number" /></th>
            <th><g:message code="b2b.salesOrderDetail.openDetail" default="Open Detail" /></th>
            <th><g:message code="b2b.salesOrderDetail.product" default="Product" /></th>
            <th><g:message code="b2b.product.name" default="Product Name" /></th>
            <th><g:message code="b2b.product.standard" default="Standard" /></th>
            <th><g:message code="b2b.product.category" default="Category" /></th>
            <th><g:message code="b2b.salesOrderDetail.price" default="Price" /></th>
            <th><g:message code="b2b.salesOrderDetail.discount" default="Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.quantity" default="Order Quantity" /></th>
            <th><g:message code="b2b.salesOrderDetail.deliveryLimitation" default="Delivery Limitation" /></th>
            <th><g:message code="b2b.salesOrderDetail.amount" default="Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.contractDetail" default="Contract Detail" /></th>
            <th><g:message code="b2b.salesOrderDetail.specialDiscount" default="Special Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.specialAmount" default="Special Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalDiscount" default="Final Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalAmount" default="Final Amount" /></th>
        </tr>
    </thead>
    <tbody id="salesOrderDetailList">
    <g:set var="_total_child_special_amount" value="${0}"></g:set>
    <g:set var="_total_child_final_amount" value="${0}"></g:set>

    <g:each in="${salesOrderInstance.salesOrderDetails}" status="i" var="salesOrderDetailInstance">
    	<!-- category -->
    	<g:set 
            var="category" 
            value="${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailInstance?.product?.serialNumber] )}" />
        <!-- rowspan -->
        <g:set var="rowspan" value="${0}" />
       	<g:if test="${salesOrderDetailInstance?.salesOrderDetailDetails?.size() > 0}">
            <g:set var="rowspan" value="${salesOrderDetailInstance?.salesOrderDetailDetails?.size() + 1}" />
       	</g:if>
    
        <tr class="prop ${(i % 2) == 0 ? 'odd' : 'even'} " childs="${salesOrderDetailInstance?.serialNumber}">
	       	<!-- serial number -->
        	<td ${(rowspan > 0)?('rowspan="'+rowspan+'"'):('') } class="${(rowspan > 0)?'trparent':'' }">
            	${fieldValue(bean: salesOrderDetailInstance, field: "serialNumber")}
            </td>
            <!-- open detail -->
            <td class="${(rowspan > 0)?'trparent':'' }"><g:formatBoolean boolean="${(salesOrderDetailInstance?.openDetail)?:false}"></g:formatBoolean></td>
            <!-- product -->
            <td class="${(rowspan > 0)?'trparent':'' }">${fieldValue(bean: salesOrderDetailInstance, field: "product")}</td>
            <!-- product.name -->
            <td class="${(rowspan > 0)?'trparent':'' }">${fieldValue(bean: salesOrderDetailInstance, field: "product.name")}</td>
            <!-- product.standard -->
            <td class="${(rowspan > 0)?'trparent':'' }">${fieldValue(bean: salesOrderDetailInstance, field: "product.standard")}</td>
            <!-- category -->
            <td class="${(rowspan > 0)?'trparent':'' }">${category }</td>
            <!-- price -->
            <td class="${(rowspan > 0)?'trparent':'' } number"><g:formatNumber number="${salesOrderDetailInstance?.price}" format="###,##0.000000" /></td>
            <!-- discount -->
            <td class="${(rowspan > 0)?'trparent':'' } number"><g:formatNumber number="${salesOrderDetailInstance?.discount}" format="###,##0.00%" /></td>
            <!-- quantity -->
            <td class="${(rowspan > 0)?'trparent':'' } number"><g:formatNumber number="${salesOrderDetailInstance?.quantity}" format="###,##0.00" /></td>
            <!-- delivery limitation -->
            <td class="${(rowspan > 0)?'trparent':'' }"><g:formatDate format="yyyy-MM-dd" date="${salesOrderDetailInstance?.deliveryLimitation}"/></td>
            <!-- amount -->
            <td class="${(rowspan > 0)?'trparent':'' } number"><g:formatNumber number="${salesOrderDetailInstance?.amount}" format="###,##0.0000" /></td>
            <!-- contract Detail -->
            <td class="${(rowspan > 0)?'trparent':'' }">
            	<g:link controller="contract" action="show" id="${salesOrderDetailInstance?.contractDetail?.contract?.id}">${fieldValue(bean: salesOrderDetailInstance, field: "contractDetail")}</g:link>
            </td>
            <!-- special discount -->
            <td class="${(rowspan > 0)?'trparent':'' } number">
            	<g:if test="${salesOrderDetailInstance?.openDetail == true}">
            		<span class="override_number">
	            		<g:formatNumber number="${(salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum() ?: 0)/(salesOrderDetailInstance?.amount ?: 1)}" format="###,##0.00%" />
	            	</span>
            	</g:if>
            	<g:else>
	            	<g:formatNumber number="${salesOrderDetailInstance?.specialDiscount}" format="###,##0.00%" />
            	</g:else>
            </td>
            <!-- special amount -->
            <td class="${(rowspan > 0)?'trparent':'' } number">
            	<g:if test="${salesOrderDetailInstance?.openDetail == true}">
           		    <g:set var="_total_child_special_amount" value="${(_total_child_special_amount ?: 0) + (salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum() ?: 0)}"></g:set>
            		<span class="override_number">
	            		<g:formatNumber number="${salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum()?:0}" format="###,##0.0000" />
	            	</span>
            	</g:if>
            	<g:else>
	       		    <g:set var="_total_child_special_amount" value="${(_total_child_special_amount ?: 0) + (salesOrderDetailInstance?.specialAmount ?: 0)}"></g:set>
	            	<g:formatNumber number="${salesOrderDetailInstance?.specialAmount}" format="###,##0.0000" />
            	</g:else>
            </td>
            <!-- final discount -->
            <td class="${(rowspan > 0)?'trparent':'' } number">
            	<g:if test="${salesOrderDetailInstance?.openDetail == true}">
	            	<span class="override_number">
						<g:formatNumber 
							number="${((salesOrderDetailInstance?.amount ?: 0) - (salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum() ?: 0))/(salesOrderDetailInstance?.amount ?: 1)}" 
							format="###,##0.00%" />
						</span>
            	</g:if>
            	<g:else>
	            	<g:formatNumber number="${salesOrderDetailInstance?.finalDiscount}" format="###,##0.00%" />
            	</g:else>
            </td>
            <!-- final amount -->
            <td class="${(rowspan > 0)?'trparent':'' } number">
            	<g:if test="${salesOrderDetailInstance?.openDetail == true}">
           		    <g:set 
						var="_total_child_final_amount" 
						value="${(_total_child_final_amount ?: 0) + (salesOrderDetailInstance?.amount ?: 0) - (salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum() ?: 0)}"></g:set>
	            	<span class="override_number">
						<g:formatNumber number="${(salesOrderDetailInstance?.amount ?: 0) - (salesOrderDetailInstance?.salesOrderDetailDetails*.specialAmount?.sum() ?: 0)}" format="###,##0.0000" /></span>
            	</g:if>
            	<g:else>
	       		    <g:set 
						var="_total_child_final_amount" 
						value="${(_total_child_final_amount ?: 0) + (salesOrderDetailInstance?.finalAmount ?: 0)}"></g:set>
	            	<g:formatNumber number="${salesOrderDetailInstance?.finalAmount}" format="###,##0.0000" />
            	</g:else>
            </td>
       </tr>
	    <g:each in="${salesOrderDetailInstance.salesOrderDetailDetails}" status="j" var="salesOrderDetailDetailInstance">
	    	<g:set 
                var="category" 
                value="${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailDetailInstance?.product?.serialNumber] )}" />
		    <tr class="prop ${(i % 2) == 0 ? 'odd' : 'even'} " parent="${salesOrderDetailInstance?.serialNumber}">
		    	<!-- serial number -->
	            <td>(+)${fieldValue(bean: salesOrderDetailDetailInstance, field: "serialNumber")}</td>
	            <!-- product -->
	            <td>${fieldValue(bean: salesOrderDetailDetailInstance, field: "product")}</td>
	            <!-- product.name -->
	            <td>${fieldValue(bean: salesOrderDetailDetailInstance, field: "product.name")}</td>
	            <!-- product.standard -->
	            <td>${fieldValue(bean: salesOrderDetailDetailInstance, field: "product.standard")}</td>
	            <!-- category -->
	            <td>${category}</td>
	            <!-- price -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.price}" format="###,##0.000000" /></td>
	            <!-- discount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.discount}" format="###,##0.00%" /></td>
	            <!-- quantity -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.quantity}" format="###,##0.000" /></td>
	            <!-- blank -->
	            <td>&nbsp;</td>
	            <!-- amount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.amount}" format="###,##0.0000" /></td>
	            <!-- contract Detail -->
	            <td>
	            	<g:link controller="contract" action="show" id="${salesOrderDetailDetailInstance?.contractDetail?.contract?.id}">
							${fieldValue(bean: salesOrderDetailDetailInstance, field: "contractDetail")}
					</g:link>
	            </td>
	            <!-- special discount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.specialDiscount}" format="###,##0.00%" /></td>
	            <!-- special amount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.specialAmount}" format="###,##0.0000" /></td>
	            <!-- final discount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.finalDiscount}" format="###,##0.00%" /></td>
	            <!-- final amount -->
	            <td class="number"><g:formatNumber number="${salesOrderDetailDetailInstance?.finalAmount}" format="###,##0.0000" /></td>
	        </tr>
		    </g:each>
	    </g:each>
    </tbody>
    <tfoot>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.amount.sum" default="Total Amount" /></label>
            </td>
            <td class="number" id="totalAmount">
				${formatNumber(number:salesOrderInstance?.salesOrderDetails*.amount?.sum(), format:'###,##0.0000') }
            </td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalSpecialAmount"><g:message code="b2b.salesOrder.salesOrderDetails.specialAmount.sum" default="Total Special Amount" /></label>
            </td>
            <td class="number" id="totalSpecialAmount">
				${formatNumber(number:(_total_child_special_amount ?: 0), format:'###,##0.0000') }
            </td>
            <td>
            	<label for="totalFinalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.finalAmount.sum" default="Total Final Amount" /></label>
            </td>
            <td class="number" id="totalFinalAmount">
				${formatNumber(number:_total_child_final_amount ?: 0, format:'###,##0.0000') }
            </td>
        </tr>
    </tfoot>
</table>
<g:render template="/b2b/salesOrder/script_toggle" />
